<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="商务id" prop="ownerId">
        <el-input v-model="queryParams.ownerId" placeholder="请输入商务id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>


      <el-form-item label="商务信息" prop="ownerId" filterable>
        <el-select v-model="queryParams.ownerId" clearable placeholder="请选择机构商务" filterable>
          <el-option v-for="(item, i) in extData.ownerInfoList || []" :key="i" :label="item.nickname"
            :value="item.userId">
          </el-option>
        </el-select>.
        <div class="."></div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['merchant:applyInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['merchant:applyInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['merchant:applyInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['merchant:applyInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="applyInfoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="Id" align="center" prop="id" /> -->

      <el-table-column label="日期" align="center" prop="dateInfo" />

      <!-- <el-table-column label="手机号" align="center" prop="mobile" /> -->
      <!-- <el-table-column label="${comment}" align="center" prop="tokenInfo" /> -->
      <el-table-column label="姓名/手机号" align="center" prop="realname">
        <template slot-scope="scope">
          {{ scope.row.realname }} / {{ scope.row.mobile }}
        </template>
      </el-table-column>

      <!-- <el-table-column label="身份证头像" align="center" prop="idcardHeadUrl" />
      <el-table-column label="身份证国徽 " align="center" prop="idcardNationUrl" /> -->

      <!-- <el-table-column label="营业执照" align="center" prop="businessLicenseArray" />
      <el-table-column label="门店视频" align="center" prop="storeVideoUrl" /> -->

      <el-table-column label="公司名称" align="center" prop="companyName" />

      <!-- <el-table-column label="公司地址" align="center" prop="companyAddress" /> -->

      <!-- <el-table-column label="公司负责人" align="center" prop="companyPerson" />
      <el-table-column label="负责人电话" align="center" prop="companyPhone" /> -->

      <el-table-column label="公司logo" align="center" prop="companyLogo">
        <template slot-scope="scope">
          <ImagePreview v-if="!!scope.row.companyLogo" :src="scope.row.companyLogo" :width="70" :height="80" />
        </template>
      </el-table-column>

      <!-- <el-table-column label="${comment}" align="center" prop="otherInfo" /> -->

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status === -1" :style="{ color: 'red' }">
            未通过 <br /><span v-if="!!scope.row.tipsMsg">提示：{{ scope.row.tipsMsg }}</span>
          </span>
          <span v-if="scope.row.status === 0" :style="{ color: 'black' }">
            待审核 <br /><span v-if="!!scope.row.tipsMsg">提示：{{ scope.row.tipsMsg }}</span>
          </span>
          <span v-if="scope.row.status === 1" :style="{ color: 'green' }">
            通过
          </span>
          <span v-if="scope.row.status === -2" :style="{ color: 'red' }">
            商务已查阅，等待管理员审核
          </span>
          <!-- <span v-if="scope.row.status === -1" :style="{ color: scope.row.status === -1 ? 'red' : scope.row.status === 0 ? 'black':'green' }">
            {{ scope.row.status === -1 ? '未通过' : scope.row.status === 0 ? '待审核':'通过' }}
          </span> -->
        </template>
      </el-table-column>

      <!-- <el-table-column label="商务id" align="center" prop="ownerId" /> -->
      <el-table-column label="商务" align="center">
        <template slot-scope="scope">
          {{ getOwnerName(scope.row.ownerId) }} [ {{ scope.row.ownerId }} ]
        </template>
      </el-table-column>

      <!-- <el-table-column label="操作人员ID" align="center" prop="operatorId" /> -->
      <el-table-column label="操作人员" align="center" prop="operatorName">
        <template slot-scope="scope">
          {{ scope.row.operatorName }} [ {{ scope.row.operatorId }} ]
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate1(scope.row)"
            v-hasPermi="['merchant:applyInfo:edit']" v-if="scope.row.status != 1">补充资料</el-button>
          <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['merchant:applyInfo:edit1']" v-if="scope.row.status != 1">审核</el-button>
          <el-button size="small" type="text" icon="el-icon-edit" @click="handleUpdate2(scope.row)"
            v-hasPermi="['merchant:applyInfo:edit']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商户申请信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="dateInfo">
          <el-input v-model="form.dateInfo" placeholder="请输入手机号" readonly="true" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" readonly="true" />
        </el-form-item>
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" readonly="true" />
        </el-form-item>
        <el-form-item label="身份证头像" prop="idcardHeadUrl">
          <!-- <el-input v-model="form.idcardHeadUrl" placeholder="请输入身份证头像" /> -->

          <ImagePreview v-if="!!form.idcardHeadUrl" :src="form.idcardHeadUrl" :width="70" :height="80" />
        </el-form-item>
        <el-form-item label="身份证国徽 " prop="idcardNationUrl">
          <!-- <el-input v-model="form.idcardNationUrl" placeholder="请输入身份证国徽 " /> -->
          <ImagePreview v-if="!!form.idcardNationUrl" :src="form.idcardNationUrl" :width="70" :height="80" />
        </el-form-item>

        <el-form-item label="营业执照" prop="businessLicenseArray">
          <!-- <el-input v-model="form.businessLicenseArray" type="textarea" placeholder="请输入内容" /> -->
          <span v-for="(url, index) in form.bsArray" :key="index">
            <ImagePreview :src="url" :width="70" :height="80" />
          </span>
        </el-form-item>
        <el-form-item label="门店视频" prop="storeVideoUrl">
          <!-- <el-input v-model="form.storeVideoUrl" placeholder="请输入门店视频" /> -->
          <video :src="form.storeVideoUrl" controls :width="280" :height="280"></video>
        </el-form-item>
        <el-form-item label="机构名称" prop="infoName">
          <el-input v-model="form.infoName" placeholder="请输入机构名称" :maxlength="6" show-word-limit />
        </el-form-item>

        <el-form-item label="机构类型" prop="infoType">
          <el-select v-model="form.infoType" placeholder="请选择机构类型">
            <el-option label="CRM" value="CRM"></el-option>
            <el-option label="API" value="API"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" readonly="true" />
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入公司地址" readonly="true" />
        </el-form-item>
        <el-form-item label="公司负责人" prop="companyPerson">
          <el-input v-model="form.companyPerson" placeholder="请输入公司负责人" readonly="true" />
        </el-form-item>
        <el-form-item label="负责人电话" prop="companyPhone">
          <el-input v-model="form.companyPhone" placeholder="请输入负责人电话" readonly="true" />
        </el-form-item>
        <el-form-item label="公司logo" prop="companyLogo">
          <ImagePreview v-if="!!form.companyLogo" :src="form.companyLogo" :width="70" :height="80" />
        </el-form-item>
        <el-form-item label="其他内容" prop="otherInfo">
          <span v-for="(url, index) in form.otherInfoArray" :key="index">
            <ImagePreview :src="url" :width="70" :height="80" />
          </span>
        </el-form-item>


        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="未通过" :value="-1"></el-option>
            <el-option label="待审核" :value="0"></el-option>
            <el-option label="商务已查阅" :value="-2"></el-option>
            <el-option label="通过" :value="1"></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="提示信息" prop="tipsMsg">
          <el-input v-model="form.tipsMsg" type="textarea" placeholder="请输入提示信息" />
        </el-form-item>

        <el-form-item label="合同地址" prop="contractUrl">
          <el-input v-model="form.contractUrl" type="textarea" placeholder="请输入合同地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <el-dialog :title="title" :visible.sync="openone" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="dateInfo">
          <el-input v-model="form.dateInfo" placeholder="请输入手机号" readonly />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" readonly />
        </el-form-item>
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" readonly />
        </el-form-item>
        <el-form-item label="身份证头像" prop="idcardHeadUrl">
          <ImagePreview v-if="!!form.idcardHeadUrl" :src="form.idcardHeadUrl" :width="70" :height="80" />
        </el-form-item>
        <el-form-item label="身份证国徽" prop="idcardNationUrl">
          <ImagePreview v-if="!!form.idcardNationUrl" :src="form.idcardNationUrl" :width="70" :height="80" />
        </el-form-item>

        <el-form-item label="营业执照" prop="businessLicenseArray">
          <span v-for="(url, index) in form.bsArray" :key="index">
            <ImagePreview :src="url" :width="70" :height="80" />
          </span>
        </el-form-item>
        <el-form-item label="门店视频" prop="storeVideoUrl">
          <video :src="form.storeVideoUrl" controls :width="280" :height="280"></video>
        </el-form-item>

        <!-- 机构名称和机构类型是可编辑的 -->
        <el-form-item label="机构名称" prop="infoName" :rules="[{ required: true, message: '请输入机构名称', trigger: 'blur' }]">
          <el-input v-model="form.infoName" placeholder="请输入机构名称" :maxlength="6" show-word-limit />
        </el-form-item>

        <el-form-item label="机构类型" prop="infoType" :rules="[{ required: true, message: '请选择机构类型', trigger: 'change' }]">
          <el-select v-model="form.infoType" placeholder="请选择机构类型">
            <el-option label="CRM" value="CRM"></el-option>
            <el-option label="API" value="API"></el-option>
          </el-select>
        </el-form-item>





        <el-form-item label="商务姓名" prop="ownerId">
          <el-select v-model="form.ownerId" style="width: 100%;" placeholder="请选择机构商务">
            <el-option v-for="(item, i) in extData.ownerInfoList || []" :key="i" :label="item.nickname"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>

        <!-- 其他字段变为只读 -->
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" readonly />
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入公司地址" readonly />
        </el-form-item>
        <el-form-item label="公司负责人" prop="companyPerson">
          <el-input v-model="form.companyPerson" placeholder="请输入公司负责人" readonly />
        </el-form-item>
        <el-form-item label="负责人电话" prop="companyPhone">
          <el-input v-model="form.companyPhone" placeholder="请输入负责人电话" readonly />
        </el-form-item>
        <el-form-item label="公司logo" prop="companyLogo">
          <ImagePreview v-if="!!form.companyLogo" :src="form.companyLogo" :width="70" :height="80" />
        </el-form-item>
        <el-form-item label="其他内容" prop="otherInfo">
          <span v-for="(url, index) in form.otherInfoArray" :key="index">
            <ImagePreview :src="url" :width="70" :height="80" />
          </span>
        </el-form-item>


        <!-- 合同地址和提示信息是可编辑的 -->
        <el-form-item label="提示信息" prop="tipsMsg">
          <el-input v-model="form.tipsMsg" type="textarea" placeholder="请输入提示信息" />
        </el-form-item>
        <el-form-item label="合同地址" prop="contractUrl">
          <el-input v-model="form.contractUrl" type="textarea" placeholder="请输入合同地址" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="待审核" :value="0"></el-option>
            <el-option label="商务已查阅" :value="-2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>



      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openone1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" readonly />
        </el-form-item>
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" readonly />
        </el-form-item>
        <el-form-item label="营业执照" prop="businessLicenseArray">
          <span v-for="(url, index) in form.bsArray" :key="index">
            <ImagePreview :src="url" :width="70" :height="80" />
          </span>
        </el-form-item>

        <el-form-item label="机构名称" prop="infoName">
          <el-input v-model="form.infoName" placeholder="请输入机构名称" readonly />
        </el-form-item>
        <el-form-item label="机构类型" prop="infoType">
          <el-select v-model="form.infoType" placeholder="请选择机构类型" disabled>
            <el-option label="CRM" value="CRM"></el-option>
            <el-option label="API" value="API"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" readonly />
        </el-form-item>
        <el-form-item label="公司地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入公司地址" readonly />
        </el-form-item>
        <el-form-item label="公司负责人" prop="companyPerson">
          <el-input v-model="form.companyPerson" placeholder="请输入公司负责人" readonly />
        </el-form-item>
        <el-form-item label="负责人电话" prop="companyPhone">
          <el-input v-model="form.companyPhone" placeholder="请输入负责人电话" readonly />
        </el-form-item>
        <el-form-item label="公司logo" prop="companyLogo">
          <ImagePreview v-if="!!form.companyLogo" :src="form.companyLogo" :width="70" :height="80" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listApplyInfo,
  getApplyInfo,
  delApplyInfo,
  addApplyInfo,
  updateApplyInfo,
} from "@/api/merchant/applyInfo";
import {
  baseInfoExtData
} from "@/api/merchant/baseInfo";

export default {
  name: "ApplyInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户申请信息表格数据
      applyInfoList: [],
      extData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openone: false,
      openone1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobile: null,
        tokenInfo: null,
        realname: null,
        idcardHeadUrl: null,
        idcardNationUrl: null,
        businessLicenseArray: null,
        storeVideoUrl: null,
        companyName: null,
        companyAddress: null,
        companyPerson: null,
        companyPhone: null,
        companyLogo: null,
        otherInfo: null,
        ownerId: null,
        ownerName: null,
        status: null,
        operatorId: null,
        operatorName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],

      },
      // showBaseInfo: 0,// 是否展示基本信息框
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },

  methods: {
    /** 查询商户申请信息列表 */
    // statusChange(event) {
    //   console.log(event)
    //   if (event === '1') {
    //     this.showBaseInfo = 1;
    //   } else {
    //     this.showBaseInfo = 0;
    //   }
    // },
    getOptions() {
      baseInfoExtData().then((response) => {
        this.extData = response.data;
      });
    },
    getOwnerName(ownerId) {
      const owner = this.extData.ownerInfoList.find(item => item.userId === ownerId)
      return owner ? owner.nickname : ownerId
    },
    getList() {
      this.loading = true;
      listApplyInfo(this.queryParams).then((response) => {
        this.applyInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openone = false;
      this.openone1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mobile: null,
        tokenInfo: null,
        realname: null,
        idcardHeadUrl: null,
        idcardNationUrl: null,
        businessLicenseArray: null,
        storeVideoUrl: null,
        companyName: null,
        companyAddress: null,
        companyPerson: null,
        companyPhone: null,
        companyLogo: null,
        otherInfo: null,
        ownerId: null,
        ownerName: null,
        status: 0,
        operatorId: null,
        operatorName: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户申请信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getApplyInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户申请信息";
      });
    },
    handleUpdate1(row) {
      this.reset();
      const id = row.id || this.ids;
      getApplyInfo(id).then((response) => {
        this.form = response.data;
        this.openone = true;
        this.title = "补充客户资料";
      });
    },
    handleUpdate2(row) {
      this.reset();
      const id = row.id || this.ids;
      getApplyInfo(id).then((response) => {
        this.form = response.data;
        this.openone1 = true;
        this.title = "补充客户资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateApplyInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.openone = false;
              this.getList();
            });
          } else {
            addApplyInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.openone = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商户申请信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delApplyInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "merchant/applyInfo/export",
        {
          ...this.queryParams,
        },
        `applyInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
